#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
	float width, height;
	width = 600;
	height = 800;
	initgraph(width, height);
	 
	float radius, ball_x, ball_y, ball_vy, gravity;
	radius = 20;
	ball_x = width / 4;
	ball_y =0;
	ball_vy = 0;
	gravity = 0.6;
	float rect1_left_x, rect1_top_y, rect1_width, rect1_height;
	float rect2_left_x, rect2_top_y, rect2_width, rect2_height;
	float rect1_vx,rect2_vx;
	rect1_height = 300; rect2_height = 300;
	rect1_width = 20; rect2_width = 20;
	rect1_left_x = width * 3 / 4;
	rect2_left_x = width * 3 / 4;
	rect1_top_y = height - rect1_height;
	rect1_top_y = 0;
	rect1_vx = -2;
	rect2_vx = -2;

	int score = 0;
	while (1)
	{
		if (_kbhit())
		{
			char input = _getch();
			if (input = ' ')
			{
				ball_vy = -11;
			}
		}
		ball_vy = ball_vy + gravity;
		ball_y = ball_y + ball_vy;
		if (ball_y >= height - radius)
		{
			ball_y = 0;
			ball_y = height - radius;
		}
		rect1_left_x = rect1_left_x + rect1_vx;
		rect2_left_x = rect2_left_x + rect2_vx;
		if (rect1_left_x <= 0)
		{

			score = score + 1;
			rect1_left_x = width;
			rect2_left_x = width;
			rect1_height = rand() % int(height / 4) + height / 4;
			rect2_height = 600 - rect1_height;
			rect1_vx = rand() / float(RAND_MAX) * 4 - 5;
			rect2_vx = rect1_vx;
		}
		if ((rect1_left_x <= ball_x + radius)
			&& (rect1_left_x + rect1_width >= ball_x - radius)
			&& (height - rect1_height <= ball_y + radius))
		{
			score = 0;
			Sleep(50);
		}
		if((rect2_left_x <= ball_x + radius)
			&& (rect2_left_x + rect2_width >= ball_x - radius)
			&& (rect2_height >= ball_y - radius))
		{
			score = 0;
			Sleep(50);
		}
		if(ball_y==height-radius)
		{
			score = 0;
			Sleep(50);
		}
		cleardevice();
		fillrectangle(rect1_left_x, height - rect1_height, rect1_width + rect1_left_x, height);
		fillrectangle(rect2_left_x, 0, rect2_width + rect2_left_x, rect2_height);
		fillcircle(ball_x, ball_y, radius);
		TCHAR s[20];
		_stprintf_s(s, _T("%d"), score);
		settextstyle(40, 0, _T("宋体"));
		outtextxy(50, 30, s);
		Sleep(10);
	}
	_getch();
	closegraph();
	return 0;
}